<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ECharts配置管理系统</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2/dist/echarts.min.js"></script>
  <style>
    .chart-container {
      height: 400px;
      margin-bottom: 20px;
    }
    .config-editor {
      font-family: monospace;
      height: 300px;
    }
    .search-results {
      max-height: 600px;
      overflow-y: auto;
    }
  </style>
</head>
<body>
  <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
    <div class="container">
      <a class="navbar-brand" href="#">ECharts配置管理系统</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav me-auto">
          <li class="nav-item">
            <a class="nav-link" href="editor.html">
              <i class="bi bi-plus-lg"></i> 新建配置
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="configs.html">
              <i class="bi bi-list"></i> 配置列表
            </a>
          </li>
        </ul>
        <ul class="navbar-nav">
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle text-white" href="#" id="userDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              <span id="username">用户</span>
            </a>
            <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
              <li><a class="dropdown-item" href="#" id="logoutBtn">退出登录</a></li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
  </nav>

  <div class="container mt-5">
    <div class="row justify-content-center">
      <div class="col-md-8 text-center">
        <h1 class="display-4 mb-4">欢迎使用 ECharts 配置管理系统</h1>
        <p class="lead mb-5">轻松创建、管理和分享您的 ECharts 图表配置</p>
        
        <div class="row g-4">
          <div class="col-md-6">
            <div class="card h-100">
              <div class="card-body">
                <i class="bi bi-plus-circle display-4 text-primary mb-3"></i>
                <h3>新建配置</h3>
                <p>使用可视化编辑器创建新的图表配置</p>
                <a href="editor.html" class="btn btn-primary">开始创建</a>
              </div>
            </div>
          </div>
          <div class="col-md-6">
            <div class="card h-100">
              <div class="card-body">
                <i class="bi bi-collection display-4 text-primary mb-3"></i>
                <h3>配置列表</h3>
                <p>查看和管理您保存的所有图表配置</p>
                <a href="configs.html" class="btn btn-primary">查看列表</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  <script src="js/main.js"></script>
  
  <script>
    // 认证检查
    document.addEventListener('DOMContentLoaded', function() {
      // 检查是否已登录
      const token = localStorage.getItem('token');
      if (!token) {
        // 未登录，重定向到登录页面
        window.location.href = 'login.html';
        return;
      }

      // 验证令牌有效性
      fetch('/api/auth/me', {
        headers: {
          'Authorization': `Bearer ${token}`
        }
      })
      .then(response => {
        if (!response.ok) {
          throw new Error('令牌无效');
        }
        return response.json();
      })
      .then(data => {
        if (data.success && data.data) {
          // 显示用户名
          document.getElementById('username').textContent = data.data.username;
        } else {
          throw new Error('获取用户信息失败');
        }
      })
      .catch(error => {
        console.error('认证失败:', error);
        // 清除无效令牌并重定向到登录页面
        localStorage.removeItem('token');
        localStorage.removeItem('user');
        window.location.href = 'login.html';
      });

      // 退出登录
      document.getElementById('logoutBtn').addEventListener('click', function(e) {
        e.preventDefault();
        // 清除本地存储的令牌和用户信息
        localStorage.removeItem('token');
        localStorage.removeItem('user');
        // 重定向到登录页面
        window.location.href = 'login.html';
      });
    });
  </script>
</body>
</html>